﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using SundUnderviser.ModelLayer;

namespace SundUnderviser.DBLayer
{
     public class DBJournal
    {
        private SqlCommand dbCmd = null;
        
        public List<Journal> FindJournal(int pnr)
        {
            string sql = @"select patientnr, journalnr, dato, vagt, init, soegeord, planlagthandling, antaldage from journal " +
                           " where patientnr  = " + pnr
                           + " order by dato desc";


            Console.WriteLine("sql stmt = " + sql);

            dbCmd = DBConnection.GetDbCommand(sql);

            IDataReader dbReader;
            dbReader = dbCmd.ExecuteReader();
            List<Journal> journalList = new List<Journal>();
            Journal journalObj;

            while (dbReader.Read())
            {

                journalObj = new Journal();
                Console.WriteLine("antal felter " + dbReader.FieldCount);
                Patient patientObj = new Patient();
                patientObj.Nr = Convert.ToInt32(dbReader["patientnr"].ToString());
                journalObj.Patient = patientObj;
                journalObj.Nr = Convert.ToInt32(dbReader["journalnr"].ToString());
                journalObj.Dato = DateTime.Parse(dbReader["dato"].ToString());
                journalObj.Vagt = dbReader["vagt"].ToString();
                journalObj.Inti = dbReader["init"].ToString();
                journalObj.Soegord = dbReader["soegeord"].ToString();
                journalObj.PlanlagtHandling = dbReader["planlagthandling"].ToString();
                journalObj.AntalDage = Convert.ToInt32(dbReader["antaldage"].ToString());

                journalList.Add(journalObj);


            }
            dbReader.Close();
            DBConnection.Close();
            return journalList;
        }



        public int InsertJournal(Journal journalObj)
        {
            int rc = -1;
            SqlCommand dbCmd = null;
            try
            { // tildel naeste nr
                journalObj.Nr = 1;
                // 
                string sql = "select * from journal where patientnr = " + journalObj.Patient.Nr;
                SqlCommand dbCmd1 = DBConnection.GetDbCommand(sql);
                IDataReader dbReader;
                dbReader = dbCmd1.ExecuteReader();
                if (dbReader.Read())
                {
                    Console.WriteLine("har fundet nogle");
                    String sql2 = "select max(journalnr) as nextnr from journal where patientnr = " + journalObj.Patient.Nr;
                    DBConnection.Close();
                    SqlCommand dbCmd2 = DBConnection.GetDbCommand(sql2);
                    dbReader = dbCmd2.ExecuteReader();
                    Console.WriteLine("næstenr statusObj 1: " + journalObj.Nr);
                    if (dbReader.Read())
                    {
                        Console.WriteLine("tildel ny nr");
                        if (dbReader["nextnr"] != null)
                        {
                            journalObj.Nr = Convert.ToInt32(dbReader["nextnr"].ToString()) + 1;
                            Console.WriteLine("næstenr statusObj 2: " + journalObj.Nr);
                        }
                    }

                }

                DBConnection.Close();
                Console.WriteLine("Insert journal dato : " + journalObj.Dato);
                string myDato = "" + journalObj.Dato.Year;
                if (journalObj.Dato.Month >= 10)
                {
                    myDato = myDato + journalObj.Dato.Month;
                }
                else
                {
                    myDato = myDato + "0" + journalObj.Dato.Month;
                }
                              
                if (journalObj.Dato.Day >= 10)
                {
                    myDato = myDato + journalObj.Dato.Day;
                }
                else
                {
                    myDato = myDato + "0" + journalObj.Dato.Day;
                }
               
                Console.WriteLine("Insert journal mydato : " + myDato);
                string sqlInsert = "insert into journal (journalnr, patientnr, dato, vagt,init, soegeord, planlagthandling, antaldage)" +
                       "values (" + journalObj.Nr + ","
                       + journalObj.Patient.Nr + ",'" +
                        myDato + "','" +
                  //     journalObj.Dato + "','" +
                       journalObj.Vagt + "','" +
                       journalObj.Inti + "','" +
                       journalObj.Soegord + "','" +
                       journalObj.PlanlagtHandling + "'," +
                       journalObj.AntalDage + ")";
                Console.WriteLine(sqlInsert);
                dbCmd = DBConnection.GetDbCommand(sqlInsert);


                rc = dbCmd.ExecuteNonQuery();


            }
            catch (Exception insertE)
            {
                Console.WriteLine("Fejl i insert Journal ankomst");
                Console.WriteLine(insertE);
            }
            DBConnection.Close();
            return rc;
        }
    }
}
